package indi.zn.commons.compress;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Gzip  implements Compressor {

	private int buffer=4096;
	public static final String EXT = ".gz";

	public Gzip() {

	}

	public Gzip(int buffer) {
		this.buffer = buffer;
	}

	public int getBuffer() {
		return buffer;
	}

	public void setBuffer(int buffer) {
		this.buffer = buffer;
	}

	public void compress(InputStream is, OutputStream os) throws IOException {

		GZIPOutputStream gos = new GZIPOutputStream(os);
		
		int count;
		byte data[] = new byte[buffer];
		while ((count = is.read(data, 0, buffer)) != -1) {
			gos.write(data, 0, count);
		}

		gos.finish();

		gos.flush();
		gos.close();
	}

	public void decompress(InputStream is, OutputStream os) throws IOException {

		GZIPInputStream gis = new GZIPInputStream(is);

		int count;
		byte data[] = new byte[buffer];
		while ((count = gis.read(data, 0, buffer)) != -1) {
			os.write(data, 0, count);
		}

		gis.close();
	}

	public String compress(String str) throws IOException {

		String cstr = new String(compress(str.getBytes()));

		return cstr;
	}

	public String decompress(String str) throws IOException {

		String cstr = new String(decompress(str.getBytes()));

		return cstr;
	}

	public byte[] compress(byte[] data) throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(data);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		compress(bais, baos);

		byte[] output = baos.toByteArray();

		baos.flush();
		baos.close();
		bais.close();

		return output;
	}

	public byte[] decompress(byte[] data) throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(data);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		decompress(bais, baos);

		data = baos.toByteArray();

		baos.flush();
		baos.close();

		bais.close();

		return data;
	}

	public File compress(File file, boolean delete) throws IOException {

		FileInputStream fis = new FileInputStream(file);

		String npath = file.getPath().replace(EXT, "");

		FileOutputStream fos = new FileOutputStream(npath);
		decompress(fis, fos);
		fis.close();
		fos.flush();
		fos.close();

		if (delete) {
			file.delete();
		}
		return new File(npath);
	}

	public File decompress(File file, boolean delete) throws IOException {
		FileInputStream fis = new FileInputStream(file);

		String npath = file.getPath().replace(EXT, "");

		FileOutputStream fos = new FileOutputStream(file.getPath().replace(EXT,
				""));
		decompress(fis, fos);
		fis.close();
		fos.flush();
		fos.close();

		if (delete) {
			file.delete();
		}

		return new File(npath);

	}

}
